/*
 *  This file is part of WWWPal.
 *
 *  Copyright (C) 2003  Dr. John Punin, Department of Computer Science
 *  Rensselaer Polytechnic Institute
 *
 *  WWWPal is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 of the License, or
 *  (at your option) any later version.
 *
 *  WWWPal is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with Foobar; if not, write to the Free Software
 *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */

#include "rep_logs_directories.h"
#include "rep_general.h"
#include "rep_logs_pages.h"
#include "GR_write_html.h"

void
write_body_dmreq_html_file(GraphFrame *gf, FILE *fp,int ndir,
			   WebStatPtr msn);

int 
report_directories_most_accesses(GraphFrame *gf, char *name_file,int ndir,
				  WebStatPtr msn)
{
  FILE *fp = assert_open_html_file(name_file);
  if(!fp)
      return -1;
  write_head_html_file(fp,"Most Requested Directories");
  write_start_body_html_file(fp,"#ffffff");
  write_body_dmreq_html_file(gf,fp,ndir,msn);
  write_end_body_html_file(fp);
  write_end_html_file(fp);
  fclose(fp);
  return 0;
}

void
write_body_dmreq_html_file(GraphFrame *gf, FILE *fp,int ndir,WebStatPtr msn)
{
  LNode_type *ldir = LOGR_get_list_directories(msn);
  LNode_type *ptr = NULL;
  WebGenPagePtr *varray = NULL;
  int i=0;
  char ntitle[100];
  int tn=0;
  int tndir = ldir ? count_llist(ldir) : 0;
  double tsize = LOGR_get_total_site_size(msn);
  long tacc = LOGR_get_total_site_naccesses(msn);
  time_t now;

  varray = tndir ? (WebGenPagePtr *)calloc(tndir,sizeof(WebGenPagePtr)) : NULL;

  if(!varray && tndir > 0) {
      fprintf(stderr,"Out of Memory write_body_dmreq_html_file\n");
      abort();
  }

  for(ptr = ldir->back; ptr != ldir; ptr=ptr->back)
  {
    WebGenPagePtr mdir = (WebGenPagePtr)ptr->info;
    varray[i++] = mdir;
  }

  now = time(NULL);
  write_header_html_file(fp,1,"List of Directories with Most Access Count");
  write_bold_html_file(fp,"Last Update :");
  fprintf(fp, " %s",ctime(&now));
  write_break_html_file(fp);
  write_bold_html_file(fp,"Time Periods : ");  
  write_break_html_file(fp);
  write_time_periods(fp,msn);
  write_bold_html_file(fp,"Unique Directories : ");
  fprintf(fp, " %d Directories\n",tndir);
  write_break_html_file(fp);
  write_bold_html_file(fp,"Total Number of Access Count : ");
  fprintf(fp, " %ld \n",tacc);
  write_break_html_file(fp);
  write_bold_html_file(fp,"Total Size : ");
  fprintf(fp, " %.2f K Bytes\n",(float)(tsize/1024.0));
  write_horizontal_rule_html_file(fp);
  fprintf(fp,RDMAString);
  write_horizontal_rule_html_file(fp);

  if(tndir > 0) {
      
  if(ndir < tndir)
      tn = ndir;
  else
      tn = tndir;

  sprintf(ntitle,"%d Directories with Most Access Count", tn);
  write_header_html_file(fp,2,ntitle);
  write_start_simple_table_html_file(fp);
  write_header_table_string6(fp,"Num","Directory","Access Count",
			     "Percentage Access Count",
			     "Total Access Count",
			     "Bytes","Percentage Bytes");
  for(i = 1; i<=tn; i++)
  {
    WebGenPagePtr mdir= (WebGenPagePtr)
	randomized_select((GPTR *)varray,i-1,tndir-1,1,
			  LOGR_is_access_stat_gen_lower,
			  LOGR_is_access_stat_gen_bigger);
    if(mdir)
    {
	char *sdir = (char *)LOGR_get_info_gen_page(mdir);
	long scount = LOGR_get_gen_nacc_static(mdir);
	long tcount = LOGR_get_gen_naccesses(mdir);
	char snum[20],str[20], str2[20], str3[20], str4[20],str5[20];
	long ssize = LOGR_get_gen_total_bytes(mdir);
	double ave_size = (double)(ssize/tsize)*100.0;
	float ave_nacc = ((float)scount/(float)tacc)*100.0;
	sprintf(snum,"%d",i);
	sprintf(str,"%ld",scount);
	sprintf(str5,"%ld",tcount);
	sprintf(str2,"%.2f K",(float)(ssize/1024.0));
	sprintf(str3,"%.2f %s",(float)(ave_size),"&#37;"); 
	sprintf(str4,"%.2f %s",ave_nacc,"&#37;"); 
	write_row_num_string6(fp,snum,(char *)sdir,str,str4,str5,str2,str3);
    }
  }
  write_end_table_html_file(fp); }

  free(varray); varray = NULL;
}
